#include "TFUnitTest.h"
#include "TFTest.h"
#include "TFEventListener.h"
#include "TFTestAbstractFactory.h"
#include <iostream>
TFUnitTest* TFUnitTest::m_pInstance = NULL;

TFUnitTest::TFUnitTest()
{
}


TFUnitTest::~TFUnitTest()
{
	BDVector<TFTest*>::iterator it = m_vecTest.begin();
	for (; it != m_vecTest.end(); ++it) {
		if (*it != NULL) {
			delete *it;
			*it = NULL;
		}
	}
}

void TFUnitTest::AddTest(TFTest* pTest)
{
	m_vecTest.push_back(pTest);
}

TFUnitTest* TFUnitTest::GetInstance()
{
	if (m_pInstance == NULL) {
		m_pInstance = new TFUnitTest;
	}
	return m_pInstance;
}

void TFUnitTest::Destroy() {
	if (m_pInstance == NULL) {
		return;
	}
	delete m_pInstance;
	m_pInstance = NULL;
}

void TFUnitTest::RegisterTest(TFTestAbstractFactory* factory)
{
	for (int i = 0; i < factory->GetTestCount(); ++i) {
		TFTest* pTest = factory->NewTest(i);
		m_vecTest.push_back(pTest);
	}
}

void TFUnitTest::StartAllTests()
{
	BDVector<TFTest*>::iterator it = m_vecTest.begin();
	for (; it != m_vecTest.end(); ++it) {
		m_pCurrentTest = (*it);
		(*it)->Run();
	}
}

void TFUnitTest::AddEventListener(TFEventListener* listener)
{
	if (listener == NULL) {
		return;
	}

	BDVector<TFTest*>::iterator it = m_vecTest.begin();
	for (; it != m_vecTest.end(); ++it) {
		(*it)->AddEventListener(listener);
	}
}

TFTest* TFUnitTest::CurrentTest()
{
	return m_pCurrentTest;
}

void* TFUnitTest::Run(void* data)
{
	printf("TFUnitTest::Run");
	StartAllTests();
	return NULL;
}

void TFUnitTest::SkipCurrentTest()
{
	if (m_pCurrentTest == NULL) {
		return;
	}
	CurrentTest()->Skip();
}

void TFUnitTest::RemoveEventListener(TFEventListener* listener)
{

}

TFTest* TFUnitTest::GetTest(int index)
{
	if (index >= m_vecTest.size()) {
		return NULL;
	}
	return m_vecTest[index];
}

int TFUnitTest::TestCount()
{
	return m_vecTest.size();
}